package com.lysj.admin.master.common.spring.base;


import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.exception.BaseException;
import com.lysj.admin.master.common.exception.ValidatorException;
import com.lysj.admin.master.common.jwt.JwtUtil;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
public class BaseContent {

    protected static final String LIST = "/list";
    protected static final String UPDATE = "/update";
    protected static final String DELETE = "/delete";
    protected static final String LOGIC_DELETE = "/logic_delete";

    @ExceptionHandler(ValidatorException.class)
    public Resp validationExHandle(ValidatorException e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        log.error(stackTraceElements[0] + "--" + e.toString());
        return RespUtil.error(Resp.ResStatus.PARAM_ERROR, e.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    public Resp BaseExHandle(BaseException e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        log.error(stackTraceElements[0] + "--" + e.toString());
        return RespUtil.error(Resp.ResStatus.PARAM_ERROR, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Resp handleException(Exception e) {
        e.printStackTrace();
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        log.error(stackTraceElements[0] + "--" + e.toString());
        if (e instanceof BaseException) {
            BaseException myException = (BaseException) e;
            Resp resp = RespUtil.error(ParamUtil.isBlank(myException.getCode()) ? Resp.ResStatus.INNER_ERROR
                    : RespUtil.getStatusByCode(myException.getCode()), myException.getMessage());
            return resp;
        }
        return RespUtil.error(Resp.ResStatus.INNER_ERROR, e.getMessage() == null ? "oops" : e.getMessage());
    }

    public String getJwtProperty(String key){
        Map<String, Object> payload = getPayload();
        Object o = payload.get(key);
        if(o == null){
            return "";
        }
        return o.toString();
    }

    public Map<String,Object> getPayload(){
        Map<String,Object> payload = (Map<String,Object>)request.getAttribute(CommonConstant.JWT_PAYLOAD_KEY);
        if (payload == null) {
            String token = request.getHeader(CommonConstant.TOKEN_HEADER_NAME);
            if (ParamUtil.isBlank(token)) {
                throw new BaseException(Resp.ResStatus.PARAM_ERROR, "token有误");
            }
            payload = JwtUtil.getPayloadMap(token);
        }
        return payload;
    }

    @Resource
    protected HttpServletRequest request;
    @Resource
    protected HttpServletResponse response;

}
